package com.basic.library.utils;

import android.hardware.Camera;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import com.basic.library.utils.toast.ToastCompat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jiangyun
 * @description
 * @time 创建时间：2018/5/30 09:09
 */
public class CameraUtil {

    //录音权限是否开启
    public static boolean isVoicePermission() {
        try {
            AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, 22050, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, AudioRecord.getMinBufferSize(22050, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT));
            record.startRecording();
            record.getRecordingState();
//            if(recordingState == AudioRecord.RECORDSTATE_STOPPED){
//                return false;
//            }
            record.release();
        } catch (Exception e) {
            ToastCompat.show("请检查录音权限是否开启");
            return false;
        }
        return true;
    }

    //录像权限是否开启
    public static boolean isCamera() {
        Camera camera = null;
        try {
            camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
            Camera.Parameters parameters = camera.getParameters();
            camera.setParameters(parameters);
        } catch (Exception e) {
            ToastCompat.show("请检查拍照和录像权限是否开启");
            try {
                if (camera != null) {
                    camera.setPreviewCallback(null);
//                    camera.stopPreview();
                    camera.release();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return false;
        }
        try {
            if (camera != null) {
                camera.setPreviewCallback(null);
//                camera.stopPreview();
                camera.release();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /*
        * 获取拍照之后的尺寸
        */

    private Camera.Size getPictureSize(Camera.Parameters parameters) {
        List<Camera.Size> sizes =
                parameters.getSupportedPictureSizes();// 获取支持保存图片的尺寸
        int index = -1;
        // 屏幕的宽度
        int screenWidth = ScreenUtils.getWidth();
        int screenHeight = ScreenUtils.getHeight();
        ArrayList<Integer> Index = new ArrayList<>();
        for (int i = 0; i < sizes.size(); i++) {
            if (Math.abs(screenWidth - sizes.get(i).height) == 0
                    && Math.abs(screenHeight - sizes.get(i).width) == 0) {
                index = i;
                break;
            }
        }
        // 当未找到与手机分辨率相等的数值,取列表中间的分辨率
        if (index == -1) {
            for (int i = 0; i < sizes.size(); i++) {
                int w = screenWidth - sizes.get(i).height;
                int h = screenHeight - sizes.get(i).width;
                Index.add(Math.abs(w + h));
            }
            Collections.sort(Index);
            for (int i = 0; i < sizes.size(); i++) {
                int w = screenWidth - sizes.get(i).height;
                int h = screenHeight - sizes.get(i).width;
                if (Math.abs(w + h) == Index.get(0)) {
                    index = i;
                    break;
                }
            }
        }
        return sizes.get(index);
    }
}